<template>
  <el-row :gutter="24" style="margin-left: 10px">
    <el-tabs class="table-form-detail" type="border-card">
      <el-tab-pane label="操作权限">
        <el-col :span="17">
          <div style="position: absolute">
            <el-button
              size="small"
              type="primary"
              @click="addTab(editableTabsValue)"
            >
              新增权限组
            </el-button>
          </div>

          <el-tabs
            v-model="editableTabsValue"
            type="card"
            tab-position="left"
            closable
            @tab-remove="removeTab"
          >
            <el-tab-pane
              v-for="item in editableTabs"
              :key="item.name"
              :label="item.title"
              :name="item.name"
            >
              <TableMenu />
            </el-tab-pane>
          </el-tabs>
        </el-col>
        <el-col :span="7" class="table-form-approve">
          <div>
            <p>权限组内账号</p>
            <div class="search-user">
              <span>添加用户</span>
              <el-select
                v-model="input3"
                placeholder="请输入内容"
                size="medium"
                filterable
              >
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </div>
            <TableAndButton
              list-loading="listLoading"
              :table-data="list"
              sort-change="sortChange"
              show-reviewer="false"
              :column-data="data.tableColumn"
              :operate-button="false"
            />
          </div>
        </el-col>
      </el-tab-pane>
      <el-tab-pane label="业务数据权限"><EquipmentDetail /></el-tab-pane>
    </el-tabs>
  </el-row>
</template>

<script>
import TableMenu from './tablemenu'
import EquipmentDetail from './equipmentdetail'
import TableAndButton from '@/components/TableAndButton'
import data from './const'
import { fetchRoleList } from '@/api/system'
export default {
  components: { TableMenu, TableAndButton, EquipmentDetail },
  data() {
    return {
      tableKey: 1,
      data,
      menu1: true,
      input1: '',
      input2: '',
      input3: '',
      total: '',
      listLoading: '',
      list: [],
      options: [
        {
          value: '选项1',
          label: '黄金糕'
        },
        {
          value: '选项2',
          label: '双皮奶'
        },
        {
          value: '选项3',
          label: '蚵仔煎'
        },
        {
          value: '选项4',
          label: '龙须面'
        },
        {
          value: '选项5',
          label: '北京烤鸭'
        }
      ],
      editableTabsValue: '2',
      editableTabs: [
        {
          title: '权限组1',
          name: '1',
          content: 'Tab 1 content'
        },
        {
          title: '权限组2',
          name: '2',
          content: 'Tab 2 content'
        }
      ],
      tabIndex: 2
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    getRoleList() {
      fetchRoleList().then(response => {
        this.list = response.data.items
        this.total = response.data.total
        this.listLoading = false
        // Just to simulate the time of the request
      })
    },
    addTab(targetName) {
      const newTabName = ++this.tabIndex + ''
      this.editableTabs.push({
        title: '新增权限组',
        name: newTabName,
        content: '新增权限组'
      })
      this.editableTabsValue = newTabName
    },
    removeTab(targetName) {
      const tabs = this.editableTabs
      let activeName = this.editableTabsValue
      if (activeName === targetName) {
        tabs.forEach((tab, index) => {
          if (tab.name === targetName) {
            const nextTab = tabs[index + 1] || tabs[index - 1]
            if (nextTab) {
              activeName = nextTab.name
            }
          }
        })
      }

      this.editableTabsValue = activeName
      this.editableTabs = tabs.filter(tab => tab.name !== targetName)
    }
  }
}
</script>
<style>
.el-tabs--left .el-tabs__header.is-left {
  margin-top: 60px;
}
.search-user {
  margin: 20px 0;
}
</style>
